package com.meilingcloud.activiti.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.meilingcloud.activiti.domain.ActWfShift;
import com.meilingcloud.activiti.domain.dto.ActWfShiftSaveDto;
import com.meilingcloud.activiti.service.IActWfShiftService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 转移单据 Controller
 *
 * @author chenl
 * @date 2021-04-30
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/activiti/shift")
public class ActWfShiftController extends BaseController {

    private final IActWfShiftService iActWfShiftService;

    /**
     * 查询当前用户转移单据 列表
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActWfShift actWfShift) {
        actWfShift.setCreateBy(SecurityUtils.getUsername());
        startPage();
        List<ActWfShift> list = iActWfShiftService.queryShiftWithTaskList(actWfShift);
        return getDataTable(list);
    }

    /**
     * 查询当前部门下所有转移单据
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ActWfShift actWfShift) {
        List<String> userNames = iActWfShiftService.queryUserByDept();
        startPage();
        List<ActWfShift> list = iActWfShiftService.queryShiftInDeptList(actWfShift, userNames);
        return getDataTable(list);
    }

    /**
     * 导出转移单据 列表
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:export')")
    @Log(title = "转移单据 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActWfShift actWfShift) {
        List<ActWfShift> list = iActWfShiftService.queryList(actWfShift);
        ExcelUtil<ActWfShift> util = new ExcelUtil<ActWfShift>(ActWfShift.class);
        return util.exportExcel(list, "shift");
    }

    /**
     * 获取转移单据 详细信息
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:query')")
    @GetMapping(value = "/{shiftId}")
    public AjaxResult getInfo(@PathVariable("shiftId") String shiftId) {
        return AjaxResult.success(iActWfShiftService.getShiftDetail(shiftId));
    }

    /**
     * 根据实例id获取单据详细信息
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:query')")
    @GetMapping(value = "/byInstanceId/{instanceId}")
    public AjaxResult getInfoByInstanceId(@PathVariable("instanceId") String instanceId) {
        return AjaxResult.success(iActWfShiftService.getOne(new LambdaQueryWrapper<ActWfShift>().eq(ActWfShift::getInstanceId, instanceId)));
    }

    /**
     * 新增转移单据
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:add')")
    @Log(title = "转移单据 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActWfShiftSaveDto actWfShiftSaveDto) {
        return toAjax(iActWfShiftService.saveWfShift(actWfShiftSaveDto));
    }

    /**
     * 撤销流程
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:edit')")
    @Log(title = "转移单据撤销 ", businessType = BusinessType.OTHER)
    @PostMapping(value = "/revoke")
    public AjaxResult revoke(@RequestBody String businessKey) {
        return toAjax(iActWfShiftService.revoke(businessKey));
    }

    /**
     * 修改转移单据
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:edit')")
    @Log(title = "转移单据 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActWfShift actWfShift) {
        return toAjax(iActWfShiftService.updateById(actWfShift));
    }

    /**
     * 删除转移单据
     */
    @PreAuthorize("@ss.hasPermi('activiti:shift:remove')")
    @Log(title = "转移单据 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shiftIds}")
    public AjaxResult remove(@PathVariable String[] shiftIds) {
        return toAjax(iActWfShiftService.removeByIds(Arrays.asList(shiftIds)));
    }
}
